package com.example.liming39.myapplication.feature;

import android.Manifest;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.Toast;

import static com.example.liming39.myapplication.feature.MyApp.context;

/**
 * @author liming356
 * @since 2018/10/18/018
 */
public class MyService extends Service {

    private LocationManager locationManager;

    @Override
    public IBinder onBind(Intent intent) {
        throw new UnsupportedOperationException("Not yet implemented");
        //return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        new Thread() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(3000);
                    } catch (Exception e) {}
                    showToast("app is running");
                }
            }
        }.start();
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        if (Build.VERSION.SDK_INT >= 23 &&
                ContextCompat.checkSelfPermission( context, android.Manifest.permission.ACCESS_FINE_LOCATION ) != PackageManager.PERMISSION_GRANTED &&
                ContextCompat.checkSelfPermission( context, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        final Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, 1, new LocationListener() {
            @Override//每隔0.5秒获取一次GPS定位信息，改变的最小距离1米
            public void onLocationChanged(Location location) {
                //当坐标改变时候触发该函数，如果Provider传相同的坐标，它就不会触发。
                update(location);
            }

            @Override
            public void onStatusChanged(String s, int status, Bundle bundle) {
                //Provider的状态在可用、暂时不可用和无服务三个状态直接切换时触发此函数
            }

            @Override
            public void onProviderEnabled(String s) {
                //GPS LocationProvider启用时触发此函数，比如GPS被打开
                update(location);
            }

            @Override
            public void onProviderDisabled(String s) {
                //GPS LocationProvider禁用时触发此函数，比如GPS被关闭
            }
        });
    }


    /**
     * 位置信息改变时修改全局静态变量
     * @param newLocation
     */
    public void update(Location newLocation) {
        if (newLocation != null) {
            newLocation.getLongitude();
            newLocation.getLatitude();
            float a = newLocation.getBearing() / 2;
            newLocation.getSpeed();
            newLocation.getAltitude();
            showToast("位置修改，GPS时间是：" + newLocation.getTime());
        }
    }

    private static Toast toast;
    public void showToast(String content) {
        if (toast == null) {
            toast = Toast.makeText(MyApp.context, content, Toast.LENGTH_LONG);
        } else {
            toast.setText(content);
        }
        View view = toast.getView();
        view.setBackgroundColor(Color.RED);
        toast.setView(view);
        toast.show();
    }
}
